package cate.game.play.skill.passive.hero;

import cate.common.annotation.SplitParse;
import cate.common.table.d.GDFight;
import cate.common.util.SplitParser;
import cate.common.util.XT;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.skill.passive.PassiveHandler;

import java.util.List;

/**
 * 每次受到来自英雄的攻击时，获得1层【绝杀之刃】（根据buff数量展示buff，不展示的话不用）
 */
public class 绝杀之刃PH extends PassiveHandler {

	private int buff;

	private List<Item> 对应Buff;

	private String 技能标签;

	private String buff标签;

	//buff=？？&最大数量=10&对应Buff=0:??#1:??#2:??#3:??#4:??&技能标签=锋芒毕露&buff标签=锋芒毕露
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		buff = args.getInt("buff", 0);
		String listStr = args.get("对应Buff");
		对应Buff = SplitParser.parse(Item.class, listStr);
		技能标签 = args.get("技能标签");
		buff标签 = args.getString("buff标签", "绝杀之刃");
	}

	@Override
	public void afterSufferOneAttack(SkillActionCtx action, long totalDamage, boolean strike) {
		if (action.getActor().getType() != GDFight.Fighter.HERO) {
			return;
		}
		skill.owner.buff.tryAddByTid(action, buff);
		updateShowBuff(action);
	}

	@Override
	public void onOwnerMoveDone(MoveCtx move, TimeActionCtx action) {
		if (move.getSkill().cfg.containTag(技能标签)) {
			skill.owner.buff.removeBuff(action, b -> b.cfg.containTag(buff标签));
			updateShowBuff(action);
		}
	}

	@Override
	public void onDead(ActionCtx action, Fighter killer) {
		skill.owner.buff.removeBuff(action, b -> b.cfg.containTag(buff标签));
		updateShowBuff(action);
	}

	private void updateShowBuff(ActionCtx action) {
		if (XT.isBlank(对应Buff)) {
			return;
		}
		int curNum = skill.owner.buff.countBuffByTag(buff标签);
		for (Item item : 对应Buff) {
			if (item.num == curNum) {
				skill.owner.buff.tryAddByTid(action, item.buffId);
				return;
			}
		}
	}



	public static class Item {
		@SplitParse(defaultValue = "0", note = "buff数量")
		public int num;

		@SplitParse(order = 1, defaultValue = "0", note = "展示buff")
		public int buffId;
	}
}
